package com.ruoyi.devicebind.service.impl;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.devicebind.mapper.devicebindMapper;
import com.ruoyi.devicebind.domain.devicebind;
import com.ruoyi.devicebind.service.IdevicebindService;

import static java.lang.Thread.sleep;

/**
 * 设备已绑定Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-03-16
 */
@Service
public class devicebindServiceImpl implements IdevicebindService 
{
    @Autowired
    private devicebindMapper devicebindMapper;

    /**
     * 查询设备绑定解绑
     * 
     * @param correctionuserId 设备绑定解绑ID
     * @return 设备绑定解绑
     */
    @Override
    public devicebind selectdevicebindById(Long correctionuserId)
    {
        return devicebindMapper.selectdevicebindById(correctionuserId);
    }

    /**
     * 查询设备已绑定列表
     * 
     * @param devicebind 设备绑定解绑
     * @return 设备绑定解绑
     */
    @Override
    public List<devicebind> selectdevicebindList(devicebind devicebind)
    {
        return devicebindMapper.selectdevicebindList(devicebind);
    }

    /**
     * 新增设备绑定解绑
     * 
     * @param devicebind 设备绑定解绑
     * @return 结果
     */
    @Override
    public int insertdevicebind(devicebind devicebind)
    {
        devicebind.setCreateTime(DateUtils.getNowDate());
        return devicebindMapper.insertdevicebind(devicebind);
    }

    /**
     * 修改设备绑定解绑
     * 
     * @param devicebind 设备绑定解绑
     * @return 结果
     */
    @Override
    public int updatedevicebind(devicebind devicebind)
    {
        devicebind.setUpdateTime(DateUtils.getNowDate());
        return devicebindMapper.updatedevicebind(devicebind);
    }

    /**
     * 批量删除设备绑定解绑
     * 
     * @param correctionuserIds 需要删除的设备绑定解绑ID
     * @return 结果
     */
    @Override
    public int deletedevicebindByIds(Long[] correctionuserIds)
    {
        System.out.println("批量删除");
        return devicebindMapper.deletedevicebindByIds(correctionuserIds);
    }

    /**
     * 删除设备绑定解绑信息
     * 
     * @param correctionuserId 设备绑定解绑ID
     * @return 结果
     */
    @Override
    public int deletedevicebindById(Long correctionuserId)
    {
        System.out.println("删除成功");
        return devicebindMapper.deletedevicebindById(correctionuserId);
    }


    @Override
    public int unBind(Long [] correctionuserIds){
        for(int i=0; i<correctionuserIds.length; i++){
            devicebind devicebind = devicebindMapper.selectdevicebindById(correctionuserIds[i]);
            devicebind.setUpdateTime(DateUtils.getNowDate());
            devicebind.setDelFlag("1");
            devicebindMapper.updatedevicebind(devicebind);
        }

        System.out.println("解绑成功");
        return 1;
    }

//    @Override
//    public int bind(devicebind devicebind) {
////        final int result = -1;
////        devicebind.setDelFlag("2");                         //改为待绑定状态
////        devicebindMapper.updatedevicebindByImei(devicebind);
////        System.out.println("待绑定");
////
////        Timer timer = new Timer(true);
////        TimerTask task = new TimerTask() {
////            public void run() {
////                String flag = devicebindMapper.selectdevicebindByImei(devicebind.getImei()).getDelFlag();
////                if("0".equals(flag)){                             //已绑定
////                   result = 0;
////                }else{                                              //还是待绑定，则失效，重新设为未绑定
////                   // devicebind.setDelFlag("1");
////                 //   devicebindMapper.updatedevicebindByImei(devicebind);
////                }
////                System.out.println("5秒了，变为未绑定");
////            }
////        };
////        timer.schedule(task, 15000);         //5秒后执行
//
//
//
//        return 0;
//    }
}
